import {Injectable} from '@angular/core';
import {HttpService} from './http.service';
import {Headers, Http, RequestOptions} from '@angular/http';
import {Observable} from "rxjs/Observable";

import 'rxjs/add/operator/toPromise';
import {adSystemApiUrl, AliyunOssOutputUrl, appletApiUrl, tuikeSystemUrl} from '../../../config/api';

@Injectable()
export class AdOssUploadService {

  ossSign: any;

  constructor(private httpService: HttpService, private  http: Http) {
  }

  // 获取签名
  getSignature(addr: string) {
    return this.httpService.httpGetAuth(`${adSystemApiUrl}${addr}`).map(data => data);
  }

  getSignature1(addr: string, uri: string, file,el,tkaddr?) {
    let formData=null;
    if(el){
       formData = new FormData(el);
    }else{
       formData = new FormData();
    }
    formData.append('request_uri',uri);
    formData.append('file[]',file);
    let xhr = new XMLHttpRequest();
    var observable = Observable
      .create(function(observer) {
        xhr.onreadystatechange = () => {
          if(xhr.readyState == 4 && xhr.status == 200){
            observer.next(JSON.parse(xhr.responseText));
          }
        };
        if(tkaddr) {
          xhr.open('POST',`${tuikeSystemUrl}${addr}`);
        } else {
          xhr.open('POST',`${adSystemApiUrl}${addr}`);
        }
        xhr.send(formData);
      });
    return observable;

    // const body = JSON.stringify({request_uri: uri, file: file});
    // return this.httpService.httpPostAuth(`${adSystemApiUrl}${addr}`, formData, options).map(data => data);
  }

  getSignature2(addr: string, uri: string, file,el) {//推客的上传
    let formData=null;
    if(el){
       formData = new FormData(el);
    }else{
       formData = new FormData();
    }
    formData.append('request_uri',uri);
    formData.append('file[]',file);
    let xhr = new XMLHttpRequest();
    var observable = Observable
      .create(function(observer) {
        xhr.onreadystatechange = () => {
          if(xhr.readyState == 4 && xhr.status == 200){
            observer.next(JSON.parse(xhr.responseText));
          }
        };
        xhr.open('POST',`${tuikeSystemUrl}${addr}`);
        xhr.send(formData);
      });
    return observable;

    // const body = JSON.stringify({request_uri: uri, file: file});
    // return this.httpService.httpPostAuth(`${adSystemApiUrl}${addr}`, formData, options).map(data => data);
  }

  private getSuffix(filename: string): string {
    let suffix = '';
    const pos = filename.lastIndexOf('.');
    if (pos !== -1) {
      suffix = filename.substring(pos);
    }
    return suffix;
  }

  // 上传文件
  fileUpload(file: any, data: any, fileName: string, para = '') {
    const form = new FormData();
    form.append('key', data.dir + fileName);
    form.append('policy', data.policy);
    form.append('OSSAccessKeyId', data.accessid);
    form.append('success_action_status', '200');
    if (data.callback) {
      form.append('callback', data.callback);
    }
    form.append('signature', data.signature);
    form.append('file', file);
    if (para !== '') {
      form.append('x:field', para);
    }
    return this.http.post(data.host, form).map(rs => rs);
  }

  fileUpload_(file: any, des: string) {
    const url = `${appletApiUrl}/common/attachment/upload`;
    const form = new FormData();
    form.append('source', file);
    form.append('description', des);
    return this.httpService.httpPostAuth(url, form ).map(rs => rs);
  }


  // deleteImage(file: any, data: any, fileName: string, para = '') {
  //   const form = new FormData();
  //   form.append('key', data.dir + fileName);
  //   form.append('policy', data.policy);
  //   form.append('OSSAccessKeyId', data.accessid);
  //   form.append('success_action_status', '200');
  //   form.append('signature', data.signature);
  //   form.append('file', file);
  //   return this.http.post(AliyunOssOutputUrl + '?delete',form).map(rs => rs);
  //   // let xhr = new XMLHttpRequest();
  //   // xhr.open('DELETE', AliyunOssOutputUrl, true);
  //   // xhr.onload = function() {
  //   //   if (xhr.status === 200) {
  //   //
  //   //   }else {
  //   //   }
  //   // };
  //   // xhr.send(form);
  // }
}
